package cn.dnui_zjq823.dnuifood_16110100823.controller;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import cn.dnui_zjq823.dnuifood_16110100823.R;
import cn.dnui_zjq823.dnuifood_16110100823.bean.UserBean;
import cn.dnui_zjq823.dnuifood_16110100823.listener.onRetrofitListener;
import cn.dnui_zjq823.dnuifood_16110100823.model.UserModel;

public class LoginActivity extends BaseActivity implements onRetrofitListener<UserBean>,View.OnClickListener {
    private UserModel model;
    private EditText editun,editpw;
    private CheckBox auto_login;
    private Button btn_login,btn_register;
    private String username="",password="",userid="";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_login);

    }

    @Override
    public void onSuccess(UserBean object,int flag) {
        userid=object.getUserid();
        if (!userid.equals("0")){
            saveSP();
            Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
            Intent intent=new Intent(this, MainActivity.class);
            startActivity(intent);
            finish();
        }
        else
            Toast.makeText(this,"登录失败",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFalure(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.btn_login:
                username=editun.getText().toString();
                password=editpw.getText().toString();
                model=new UserModel();
                model.login(username,password,this);
                break;
            case R.id.register_ok:
                Intent intent=new Intent(this,RegisterActivity.class);
                startActivity(intent);
                break;
        }
    }

    @Override
    void initViews() {
        layout_file=R.layout.activity_login;
        setLayout(layout_file);
        editun=findViewById(R.id.editun);
        editpw=findViewById(R.id.editpw);
        auto_login=findViewById(R.id.auto_login);
        btn_login=findViewById(R.id.btn_login);
        btn_register=findViewById(R.id.register_ok);
        readSP();
//        if (auto_login.isChecked()){
//            username=editun.getText().toString();
//            password=editpw.getText().toString();
//            model=new UserModel();
//            model.login(username,password,this);
//        }
    }

    @Override
    void initEvents() {
        btn_login.setOnClickListener(this);
        btn_register.setOnClickListener(this);
    }

    @Override
    void initDatas() {

    }
    private void readSP() {
        String strname=sp.getString("name",null);
        String strpass=sp.getString("pass",null);
        Boolean auto_login=sp.getBoolean("auto_login",false);
        if (auto_login){
            editun.setText(strname);
            editpw.setText(strpass);
        }
        this.auto_login.setChecked(auto_login);
    }
    private void saveSP(){
        SharedPreferences.Editor editor=sp.edit();
        editor.putString("name",editun.getText().toString());
        editor.putString("pass",editpw.getText().toString());
        editor.putBoolean("auto_login",auto_login.isChecked());
        editor.putString("userid",userid);
        editor.commit();
    }
}
